<?php

namespace App\Admin\Extensions;

use Carbon\Carbon;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;

class OrderExporter extends ExcelExporter implements WithColumnFormatting , WithMapping , ShouldAutoSize , WithEvents
{
    protected $fileName = '导出信息.xlsx';
    protected $headings = [
    [
        '商户ID',
        '订单编号', 
        '支付方式',
        '支付交易编号',
        '物流运单编号',
        '企业内部编号',
        '申报日期',
        '毛重（公斤）',
        '净重（公斤）',
        '序号',
        '账册备案料号',
        '商品名称',
        '数量',
        '单价',
        '代扣税款',
        '实际支付金额',
        '订购人注册号',
        '订购人姓名',
        '订购人电话',
        '省',
        '市',
        '区',
        '详细地址',
        '订购人证件号码',
        '有效期',
        '是否套装',
    ],
    [
        'ShopId',
        'OrderNO', 
        'PayWay',
        'payTransactionId',
        'logisticsNo',
        'copNo',
        'declTime',
        'grossWeight',
        'netWeight',
        'gnum',
        'itemRecordNo',
        'goodsName',
        'qty',
        'price',
        'taxTotal',
        'actualPaid',
        'buyerRegNo',
        'buyerName',
        'buyerTel',
        'province',
        'city',
        'district',
        'address',
        'buyer_id',
        'limit',
        'is_package'
    ],
    ];
    
    public function registerEvents(): array
    {
        return [
            AfterSheet::class  => function(AfterSheet $event) {
                
                //设置列宽
                // $event->sheet->getDelegate()->getColumnDimension('A')->setWidth(50);
                //设置行高，$i为数据行数
                for ($i = 0; $i<=1265; $i++) {
                    $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(20);
                }
                //设置区域单元格垂直居中
                $event->sheet->getDelegate()->getStyle('A1:Z1000')->getAlignment()->setVertical('center');
                //设置区域单元格字体、颜色、背景等，其他设置请查看 applyFromArray 方法，提供了注释
                $event->sheet->getDelegate()->getStyle('A1:Z1')->applyFromArray([
                    'fill' => [
                        'fillType' => 'linear', //线性填充，类似渐变
                        'rotation' => 45, //渐变角度
                        'startColor' => [
                            'rgb' => '32CD32' //初始颜色
                        ],
                        //结束颜色，如果需要单一背景色，请和初始颜色保持一致
                        'endColor' => [
                            'argb' => '32CD32'
                        ]
                    ]
                ]);
            }
        ];
    }

    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_GENERAL,
            'D' => NumberFormat::FORMAT_GENERAL,
            'X' => NumberFormat::FORMAT_GENERAL,
        ];
    }

    public function map($order): array
    {
        return [
            '10021',
            "\t".$order->tid,
            '北京高汇通山商业管理有限公司',
            "\t".$order->transaction,
            $order->logistics_no,//物流运单编号
            substr($order->tid , -20 , 20),//企业内部编号
            Carbon::today()->format('Ymd'),//申报日期,
            '1',//毛重
            '1',//净重
            $order->gnum,//序号,
            $order->outer_item_id,//备案料号
            $order->goods_name,//商品名称
            $order->num,//商品数量
            $order->price,//单价
            $order->tax_total,//代扣税款
            $order->payment,//实际支付款
            $order->receiver_tel,
            $order->receiver_name,
            $order->receiver_tel,
            $order->province,
            $order->city,
            $order->district,
            $order->address,
            "\t".$order->receiver_id,
            '优先老效期',
            $order->is_package == 1 ?'套装':'',

        ];
    }
    
}